﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OptionsDemo.Services
{
    public static class OrderServiceExtensions
    {
        public static IServiceCollection AddOrderService(this IServiceCollection services,IConfiguration configuration)
        {
            //services.Configure<OrderServiceOptions>(configuration);

            //需要添加验证的时候就不能使用Configure，而应该使用AddOptions
            services.AddOptions<OrderServiceOptions>().Configure(options =>
            {
                configuration.Bind(options);
            })
            //.Validate(options=>
            //{
            //    return options.MaxOrderCount <= 100;
            //},"MaxOrderCount不能大于100");
            //.ValidateDataAnnotations();
            .Services.AddSingleton<IValidateOptions<OrderServiceOptions>, OrderServiceValidateOptions>();

            //假设这里的配置方法是，等配置文件读取以后，我们的MaxOrderCount还要再加上100
            services.PostConfigure<OrderServiceOptions>(options => {
                options.MaxOrderCount += 100;
            });

            services.AddSingleton<IOrderService, OrderService>();
            return services;
        }
    }
}
